package com.kevin.blog.web.admin;

import com.kevin.blog.service.AdminCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @Author Kevin
 * @Date 2021-04-29 10:22:54
 */
@Controller
@RequestMapping("/admin")
public class AdminCommentController {

    @Autowired
    private AdminCommentService adminCommentService;

    @GetMapping("/comments")
    public String comments(@PageableDefault(size = 6, sort = {"id"},
            direction = Sort.Direction.DESC) Pageable pageable, Model model) {
        model.addAttribute("page", adminCommentService.listComment(pageable));
        return "admin/comments";
    }

    @GetMapping("/comments/{id}/delete")
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        adminCommentService.deleteComment(id);
        redirectAttributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/comments";
    }


}
